package com.yc.project2_knn;

import com.yc.project2_knn.bean.Sample;

/**
 * 欧式距离计算算法
 * 欧式距离是计算两点之间直线距离的公式，广泛应用于机器学习和数据挖掘领域。欧式距离（Euclidean Distance）是在m维空间中两个点之间的真实距离。
 * 在二维空间中，公式是 d=根号下(x1-x2)的平方+(y1-y2)的平方
 */
public class EuclideanDistanceCalculator implements DistanceCalculator {
    //父类引用指向子类对象
    @Override
    public double calculator(Sample sample1, Sample sample2) {
        double[] data1 =sample1.getExample();//样本1的数据
        double[] data2 =sample2.getExample();//样本2的数据
        double ret =0.0;

        //保证在一个数据维度
        if (data1.length !=data2.length) {
            throw  new IllegalArgumentException("样本数据维度不一致");
        }
        for (int i=0;i<data1.length;i++) {
            ret+=  Math.pow( data1[i]-data2[i],2);

        }
        return Math.sqrt(ret);
    }
}

class Student extends Sample{
    private double weight;
    private  double height;

    public Student(double weight, double height) {
        this.weight = weight;
        this.height = height;
    }

    @Override
    public String getTag() {
        return null;
    }
    @Override
    public double[] getExample() {
        return new double[]{weight,height};
    }

}